package com.wzdigit.wms.wms.service.transfer;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.SearchTransferManageRequest;
import com.wzdigit.wms.basic.client.request.transfer.UpdateTransferWhsRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageDtlGoodsResponse;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageDtlResponse;
import com.wzdigit.wms.basic.client.response.transfer.TransferManageResponse;
import com.wzdigit.wms.basic.domain.transfer.TransferDtl;
import com.wzdigit.wms.basic.domain.transfer.TransferHeader;
import com.wzdigit.wms.basic.enums.TransferDtlStatusEnum;
import com.wzdigit.wms.basic.enums.TransferHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.TransferTypeEnum;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferDtlMapper;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferHeaderMapper;
import com.wzdigit.wms.basic.util.DateUtils;
import com.wzdigit.wms.common.utils.HttpExcel;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.util.StringUtils;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.wms.service.core.transfer.TransferHeaderService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 调拨单管理-pc
 * @author Administrator
 *
 */
@Service
public class TransferManageService {
	
	@Autowired
	private TransferHeaderMapper transferHeaderMapper;
	
	@Autowired
	private TransferDtlMapper transferDtlMapper;
	
	@Autowired
	private TransferDtlGoodsMapper transferDtlGoodsMapper;
	
	@Autowired
	private TransferHeaderService transferHeaderService;
	
	public static List<String> transferTypeList;	// 调拨类型
	public static List<Integer> transferStatusList;	// 调拨状态
	static{
		transferTypeList = new ArrayList<String>(5);
        transferTypeList.add(TransferTypeEnum.T02.code);
        transferTypeList.add(TransferTypeEnum.T03.code);
        transferTypeList.add(TransferTypeEnum.T04.code);
        transferTypeList.add(TransferTypeEnum.T05.code);
        transferTypeList.add(TransferTypeEnum.T06.code);
	}
	
	static{
		transferStatusList = new ArrayList<Integer>(4);
		transferStatusList.add(TransferHeaderStatusEnum.CREATE.code);
		transferStatusList.add(TransferHeaderStatusEnum.HASDONE.code);
		transferStatusList.add(TransferHeaderStatusEnum.DOING.code);
		transferStatusList.add(TransferHeaderStatusEnum.FINISH.code);
	}

	/**
	 * 调拨单列表
	 * @param dto
	 * @return
	 */
	public PagerInfo<List<TransferManageResponse>> searchTransferManage(
			SearchTransferManageRequest dto, Pager<?> pager){
		Page<?> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<TransferManageResponse>> pagerInfo = new PagerInfo<>();
        
        Date beginDate = null, endDate = null;
        if(dto.getBeginTime() != null) {
        	beginDate = DateUtils.getStartTime(dto.getBeginTime());
        }
        if(dto.getEndTime() != null) {
        	endDate = DateUtils.getEndTime(dto.getEndTime());
        }
        if(dto.getTransferDeptName() != null) {
        	dto.setTransferDeptName(StringUtils.decode(dto.getTransferDeptName()));
        }
        
        List<TransferManageResponse> list = transferHeaderMapper.getListTransferManage(dto, transferTypeList, beginDate, endDate);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
	}
	
	/**
	 * 调拨单明细
	 * @param transferHeaderId
	 * @return
	 */
	public List<TransferManageDtlResponse> searchTransferManageDtl(
			Integer transferHeaderId){
		return transferDtlMapper.getListTransferManageDtl(transferHeaderId);
	}
	
	/**
	 * 调拨单明细SN列表
	 * @param transferDtlId
	 * @return
	 */
	public List<TransferManageDtlGoodsResponse> searchTransferManageDtlGoods(
			Integer transferHeaderId){
		return transferDtlGoodsMapper.getListTransferManageDtlGoods(transferHeaderId);
	}
	
	/**
	 * 修改 调拨单【来源仓库、目标仓库】
	 * @param dto
	 * @param servletRequest
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Result<?> updateTransferWhs(UpdateTransferWhsRequest dto, HttpServletRequest servletRequest) throws Exception {
		String userName = HttpSessionUtil.getLoginName(servletRequest);
        Date date = new Date();
        
		TransferHeader transferHeader = transferHeaderMapper.selectById(dto.getTransferHeaderId());
		transferHeader.setFmWhs(dto.getFmWhs());
		transferHeader.setToWhs(dto.getToWhs());
		transferHeader.setModifyUser(userName);
		transferHeader.setModifyTime(date);
		transferHeaderMapper.updateById(transferHeader);
		return new Result<>();
	}
	
	/**
	 * 关单
	 * @param transferNo
	 * @param servletRequest
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public Result<Boolean> close(String transferNo, HttpServletRequest servletRequest) throws Exception {
		// 调拨单头
		QueryWrapper<TransferHeader> transferHeaderQueryWrapper = new QueryWrapper<TransferHeader>();
		transferHeaderQueryWrapper.eq("TRANSFER_NO", transferNo);
		TransferHeader transferHeader = transferHeaderMapper.selectOne(transferHeaderQueryWrapper);
		if (TransferHeaderStatusEnum.DOING.getCode() != transferHeader.getStatus() &&
				TransferHeaderStatusEnum.CREATE.getCode() != transferHeader.getStatus()) {
			// 不允许关单，当前单据状态不是“进行中”或者“新建”！
			Shift.fatal(StatusCode.ONLY_DEAL_DOING_TRANSFER);
		}
		
		String userName = HttpSessionUtil.getLoginName(servletRequest);
		Date date = new Date();

		// 更新转仓单表头【状态=关闭】
		transferHeader.setModifyTime(date);
		transferHeader.setModifyUser(userName);
		transferHeader.setStatus(TransferHeaderStatusEnum.CLOSE.getCode());
		transferHeaderMapper.updateById(transferHeader);

		TransferDtl transferDtl = new TransferDtl();
		transferDtl.setTransitQty(0); // 在途数量
		transferDtl.setModifyTime(date);
		transferDtl.setModifyUser(userName);
		transferDtl.setStatus(TransferDtlStatusEnum.CLOSE.code); // 关闭

		// 调拨单明细【进行中的】
		QueryWrapper<TransferDtl> transferDtlQueryWrapper = new QueryWrapper<TransferDtl>();
		transferDtlQueryWrapper.eq("TRANSFER_NO", transferNo);
		transferDtlQueryWrapper.eq("STATUS", TransferDtlStatusEnum.DOING.getCode()); // 进行中的

		transferDtlMapper.update(transferDtl, transferDtlQueryWrapper);
		return new Result<Boolean>(true, "关闭成功！");
	}
	
	/**
	 * 导出调拨单
	 * @param dto
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void exportTransferReportList(SearchTransferManageRequest dto, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Date beginDate = null, endDate = null;
        if(dto.getBeginTime() != null) {
        	beginDate = DateUtils.getStartTime(dto.getBeginTime());
        }
        if(dto.getEndTime() != null) {
        	endDate = DateUtils.getEndTime(dto.getEndTime());
        }
        List<TransferManageResponse> dataSet = transferHeaderMapper.getListTransferManage(dto, transferTypeList, beginDate, endDate);
		
		ExportParams params = new ExportParams();
		params.setType(ExcelType.XSSF);
		params.setHeight((short)5);
		Workbook workbook = ExcelExportUtil.exportExcel(params, TransferManageResponse.class, dataSet);
		
		String fileName = "调拨单";
		HttpExcel.downLoadExcel(fileName, workbook, request, response);
	}
}
